/**
 * @param {string} paragraph
 * @param {string[]} banned
 * @return {string}
 */
/**
 * 819.最常见的单词
 * 给定一个段落 (paragraph) 和一个禁用单词列表 (banned)。返回出现次数最多，同时不在禁用列表中的单词。题目保证至少有一个词不在禁用列表中，而且答案唯一。
 * 禁用列表中的单词用小写字母表示，不含标点符号。段落中的单词不区分大小写。答案都是小写字母。
 **/
var mostCommonWord = function (paragraph, banned) {
  let reg = /\,|\.|\!|\?|\;|\:|\'/ig;
  // 将段落的标点符号全部转换为空格，字母小写并且剔除空值
  let arr = paragraph.replace(reg, " ").toLowerCase().split(' ').filter(x=>x!='')
  arr.sort();
  let result = {
    key:arr[0],
    value: 0
  }
  for (let i = 0; i < arr.length; i++) {
    let twice = 0
    for (let j = i; j<arr.length; j++ ) {
      if (arr[j] == arr[i]) {
        twice++;
      }
    }
    // 最大次数并且不在黑名单中
    if (twice > result.value && banned.indexOf(arr[i]) < 0) {
      result.value = twice;
      result.key = arr[i]
    }
  }
  return result.key;
};
(function () {
  let paragraph = "Bob hit a ball, the hit BALL flew far after it was hit."
  let banned = ["hit"]
  console.log(`最大值是${mostCommonWord(paragraph, banned)}`);
}())